<?php

namespace app\index\controller\rush;

use app\index\model\UM as UM;
use app\index\service\LawCaseRushService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use think\APP;
use think\exception\ValidateException;
use think\Request as Request;

/**
 * LawCaseRush 控制器
 * Class LawCaseRush
 * @package app\index\controller\rush;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 */
class LawCaseRush
{
    // 请求参数
    private Request $request;

    // service
    private LawCaseRushService $service;

    public function __construct(Request $request, LawCaseRushService $lawCaseRushService)
    {
        $this->request = $request;
        $this->service = $lawCaseRushService;
    }

    /**
     * 列表
     * @param UM|null $user
     * @return ApiResponse
     * @url /rush/law_case_rush/index
     */
    public function index(?UM $user): ApiResponse
    {
        $request = $this->request;

        $rush = $request->param('rush', 1);
        $cityId = $request->param('cityId');
        $categoryId = $request->param('categoryId');
        $kw = $request->param('kw');
        $page = $request->param('page', 1);
        $page_size = $request->param('page_size', 20);

        if (!$user) {
            return ApiResponse::success([]);
        }

        return ApiResponse::success($this->service->index($user, $rush, $cityId, $categoryId, $kw, $page, $page_size));
    }

    /**
     * 列表
     * @param UM|null $user
     * @return ApiResponse
     * @url /rush/law_case_rush/index
     */
    public function read(?UM $user): ApiResponse
    {
        $request = $this->request;

        $id = $request->param('id');
        if (!is_id($id)) {
            throw new ValidateException('请选择一个案源');
        }

        return ApiResponse::success($this->service->read($user, $id));
    }

    /**
     * 新增|修改
     * @param UM $user
     * @return ApiResponse
     * @url /rush/law_case_rush/save
     */
    public function save(UM $user): ApiResponse
    {
        $request = $this->request;

        if (!$request->has('id')) {
            throw new ValidateException('请选择一个案源');
        }

        $data = $this->service->modify($user, $request->param('id'), $request->param());

        return ApiResponse::success($data);
    }
}
